package com.visual.echart.controller;

import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;
import com.visual.echart.dao.MarketDataRepository;
import com.visual.echart.model.entity.MarketData;
import org.icepear.echarts.Line;
import org.icepear.echarts.Option;
import org.icepear.echarts.charts.line.LineAreaStyle;
import org.icepear.echarts.charts.line.LineSeries;
import org.icepear.echarts.components.coord.cartesian.CategoryAxis;
import org.icepear.echarts.components.coord.cartesian.ValueAxis;
import org.icepear.echarts.components.legend.Legend;
import org.icepear.echarts.components.title.Title;
import org.icepear.echarts.origin.component.title.TitleOption;
import org.icepear.echarts.origin.util.SeriesOption;
import org.icepear.echarts.render.Engine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 简介说明:echart controller
 *
 * @author: biboyuzhou
 * @date: 2025/06/15 11:01:42
 * @version:1.0
 */
@RestController
public class EChartController {
    @Autowired
    private MarketDataRepository marketDataRepository;

    public static Map<String, List<MarketData>> marketMap = new HashMap<>();
    public static final String MARKET_MAP_KEY = "marketTj";

    @GetMapping("/")
    public String index(){
        Handlebars handlebars = new Handlebars();
        String html = "";
        try {
            Template template = handlebars.compile("templates/index1");
            html = template.apply("ECharts Java1");
        } catch (IOException e) {
            System.out.println("template file not found");
        }
        return html;
    }

    @GetMapping("/volume")
    public ResponseEntity<String> getChart() {
        List<MarketData> marketDataList = marketMap.get(MARKET_MAP_KEY);
        if (CollectionUtils.isEmpty(marketDataList)) {
            marketDataList = getMarketData();
            marketMap.put(MARKET_MAP_KEY, marketDataList);
        }
        String[]  date = marketDataList.stream().map(MarketData::getDate).collect(Collectors.toList()).stream().toArray(String[]::new);
        Integer[] volume = marketDataList.stream().map(MarketData::getVolume).collect(Collectors.toList()).stream().toArray(Integer[]::new);
        Integer[] limitUp = marketDataList.stream().map(MarketData::getLimitUpNum).collect(Collectors.toList()).stream().toArray(Integer[]::new);
        Integer[] limitDown = marketDataList.stream().map(MarketData::getLimitDownNum).collect(Collectors.toList()).stream().toArray(Integer[]::new);
        Integer[] redNum = marketDataList.stream().map(MarketData::getRedNum).collect(Collectors.toList()).stream().toArray(Integer[]::new);
        Integer[] greenNum = marketDataList.stream().map(MarketData::getGreenNum).collect(Collectors.toList()).stream().toArray(Integer[]::new);
        Integer[] highest = marketDataList.stream().map(MarketData::getHighestNum).collect(Collectors.toList()).stream().toArray(Integer[]::new);
        String title = "A股大盘情绪分析";
        Legend legend = new Legend();
        legend.setData(new String[]{"量能", "涨停家数", "跌停家数", "红盘家数", "绿盘家数", "最高连板数"});
        Line line = new Line().setTitle(title)
                .setLegend(legend)
                .addXAxis(new CategoryAxis()
                        .setData(date)
                        .setBoundaryGap(false))
                .addYAxis()
                .addSeries(new LineSeries()
                        .setData(volume)
                        .setAreaStyle(new LineAreaStyle().setOpacity(0)));
        LineSeries lineSeries = new LineSeries();

        Engine engine = new Engine();
        Option option = new Option();
        TitleOption titleOption = new Title();
        titleOption.setText(title);
        option.setTitle(titleOption);
        option.setLegend(legend);
        option.setXAxis(new CategoryAxis()
                .setData(date)
                .setBoundaryGap(false));
        option.setYAxis(new ValueAxis().setType("value"));
        SeriesOption seriesOption = new LineSeries();
        seriesOption.setData(volume);
        seriesOption.setName("量能");
        seriesOption.setType("line");
        SeriesOption s1 = new LineSeries();
        s1.setData(limitUp);
        s1.setName("涨停家数");
        s1.setType("line");

        SeriesOption s2 = new LineSeries();
        s2.setData(limitDown);
        s2.setName("跌停家数");
        s2.setType("line");

        SeriesOption s3 = new LineSeries();
        s3.setData(redNum);
        s3.setName("红盘家数");
        s3.setType("line");

        SeriesOption s4 = new LineSeries();
        s4.setData(greenNum);
        s4.setName("绿盘家数");
        s4.setType("line");

        SeriesOption s5 = new LineSeries();
        s5.setData(highest);
        s5.setName("最高连板数");
        s5.setType("line");
        SeriesOption[] array = new SeriesOption[6];
        array[0] = seriesOption;
        array[1] = s1;
        array[2] = s2;
        array[3] = s3;
        array[4] = s4;
        array[5] = s5;
        option.setSeries(array);
        // return the full html of the echarts, used in iframes in your own template
        String json = engine.renderHtml(option);
        return ResponseEntity.ok(json);
    }

    @GetMapping("/limitUp")
    public ResponseEntity<String> limitUp() {
        List<MarketData> marketDataList = marketMap.get(MARKET_MAP_KEY);
        if (CollectionUtils.isEmpty(marketDataList)) {
            marketDataList = getMarketData();
            marketMap.put(MARKET_MAP_KEY, marketDataList);
        }
        String[]  date = marketDataList.stream().map(MarketData::getDate).collect(Collectors.toList()).stream().toArray(String[]::new);
        Integer[] volume = marketDataList.stream().map(MarketData::getLimitUpNum).collect(Collectors.toList()).stream().toArray(Integer[]::new);
        Line line = new Line()
                .addXAxis(new CategoryAxis()
                        .setData(date)
                        .setBoundaryGap(false))
                .addYAxis()
                .addSeries(new LineSeries()
                        .setData(volume)
                        .setAreaStyle(new LineAreaStyle().setOpacity(0)));
        Engine engine = new Engine();
        // return the full html of the echarts, used in iframes in your own template
        String json = engine.renderHtml(line);
        return ResponseEntity.ok(json);
    }

    @GetMapping("/limitDown")
    public ResponseEntity<String> limitDown() {
        List<MarketData> marketDataList = marketMap.get(MARKET_MAP_KEY);
        if (CollectionUtils.isEmpty(marketDataList)) {
            marketDataList = getMarketData();
            marketMap.put(MARKET_MAP_KEY, marketDataList);
        }
        String[]  date = marketDataList.stream().map(MarketData::getDate).collect(Collectors.toList()).stream().toArray(String[]::new);
        Integer[] volume = marketDataList.stream().map(MarketData::getLimitDownNum).collect(Collectors.toList()).stream().toArray(Integer[]::new);
        Line line = new Line()
                .addXAxis(new CategoryAxis()
                        .setData(date)
                        .setBoundaryGap(false))
                .addYAxis()
                .addSeries(new LineSeries()
                        .setData(volume)
                        .setAreaStyle(new LineAreaStyle().setOpacity(0)));
        Engine engine = new Engine();
        // return the full html of the echarts, used in iframes in your own template
        String json = engine.renderHtml(line);
        return ResponseEntity.ok(json);
    }

    @GetMapping("/redNum")
    public ResponseEntity<String> redNum() {
        List<MarketData> marketDataList = marketMap.get(MARKET_MAP_KEY);
        if (CollectionUtils.isEmpty(marketDataList)) {
            marketDataList = getMarketData();
            marketMap.put(MARKET_MAP_KEY, marketDataList);
        }
        String[]  date = marketDataList.stream().map(MarketData::getDate).collect(Collectors.toList()).stream().toArray(String[]::new);
        Integer[] volume = marketDataList.stream().map(MarketData::getRedNum).collect(Collectors.toList()).stream().toArray(Integer[]::new);
        Line line = new Line()
                .addXAxis(new CategoryAxis()
                        .setData(date)
                        .setBoundaryGap(false))
                .addYAxis()
                .addSeries(new LineSeries()
                        .setData(volume)
                        .setAreaStyle(new LineAreaStyle().setOpacity(0)));
        Engine engine = new Engine();
        // return the full html of the echarts, used in iframes in your own template
        String json = engine.renderHtml(line);
        return ResponseEntity.ok(json);
    }

    @GetMapping("/greenNum")
    public ResponseEntity<String> greenNum() {
        List<MarketData> marketDataList = marketMap.get(MARKET_MAP_KEY);
        if (CollectionUtils.isEmpty(marketDataList)) {
            marketDataList = getMarketData();
            marketMap.put(MARKET_MAP_KEY, marketDataList);
        }
        String[]  date = marketDataList.stream().map(MarketData::getDate).collect(Collectors.toList()).stream().toArray(String[]::new);
        Integer[] volume = marketDataList.stream().map(MarketData::getGreenNum).collect(Collectors.toList()).stream().toArray(Integer[]::new);
        Line line = new Line()
                .addXAxis(new CategoryAxis()
                        .setData(date)
                        .setBoundaryGap(false))
                .addYAxis()
                .addSeries(new LineSeries()
                        .setData(volume)
                        .setAreaStyle(new LineAreaStyle().setOpacity(0)));
        Engine engine = new Engine();
        // return the full html of the echarts, used in iframes in your own template
        String json = engine.renderHtml(line);
        return ResponseEntity.ok(json);
    }

    @GetMapping("/highestNum")
    public ResponseEntity<String> highestNum() {
        List<MarketData> marketDataList = marketMap.get(MARKET_MAP_KEY);
        if (CollectionUtils.isEmpty(marketDataList)) {
            marketDataList = getMarketData();
            marketMap.put(MARKET_MAP_KEY, marketDataList);
        }
        String[]  date = marketDataList.stream().map(MarketData::getDate).collect(Collectors.toList()).stream().toArray(String[]::new);
        Integer[] volume = marketDataList.stream().map(MarketData::getHighestNum).collect(Collectors.toList()).stream().toArray(Integer[]::new);
        Line line = new Line()
                .addXAxis(new CategoryAxis()
                        .setData(date)
                        .setBoundaryGap(false))
                .addYAxis()
                .addSeries(new LineSeries()
                        .setData(volume)
                        .setAreaStyle(new LineAreaStyle().setOpacity(0)));
        Engine engine = new Engine();
        // return the full html of the echarts, used in iframes in your own template
        String json = engine.renderHtml(line);
        return ResponseEntity.ok(json);
    }

    private List<MarketData> getMarketData() {
        List<MarketData> marketDataList = marketDataRepository.findAll();
        marketDataList.sort((Comparator.comparing(MarketData::getDate)));
        return marketDataList;
    }
}
